/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.LoaiNhanVienDAO;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Loainv;

/**
 *
 * @author sangnguyen
 */
public class JInternalFrameLoaiNV extends javax.swing.JInternalFrame {

    /**
     * Creates new form JInternalFrameLoaiNV
     */
    private DefaultTableModel tbModel;
    private List<Loainv> ls;
     public void XoaTrang(){
        this.jTextFieldLoaiNV.setText("");        
        this.jTextAreaGhiChu.setText("");                
    }
    public JInternalFrameLoaiNV() {
        initComponents();
        this.tbModel = new DefaultTableModel(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; 
        }
        };
        this.tbModel.addColumn("STT");
        //this.tbModel.addColumn("ID");
        this.tbModel.addColumn("Loại nhân viên");                
        this.tbModel.addColumn("Ghi chú");                
        
        ls = LoaiNhanVienDAO.SelectAllLoainv();
        for(int i=0;i<ls.size();i++)
        {
            Vector vt = new Vector();            
            vt.add(i+1);
            vt.add(ls.get(i).getLoai());
            vt.add(ls.get(i).getGhichu());            
            tbModel.addRow(vt);
        }        
        this.jTableLoaiNV.setModel(tbModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldLoaiNV = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaGhiChu = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableLoaiNV = new javax.swing.JTable();
        jButtonThoat = new javax.swing.JButton();
        jButtonClear = new javax.swing.JButton();
        jButtonXoa = new javax.swing.JButton();
        jButtonSua = new javax.swing.JButton();
        jButtonThem = new javax.swing.JButton();

        setClosable(true);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Loại Nhân Viên");

        jLabel2.setText("Loại nhân viên");

        jLabel3.setText("Ghi chú");

        jTextAreaGhiChu.setColumns(20);
        jTextAreaGhiChu.setRows(5);
        jScrollPane1.setViewportView(jTextAreaGhiChu);

        jTableLoaiNV.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableLoaiNV.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableLoaiNVMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableLoaiNV);

        jButtonThoat.setText("Thoát");
        jButtonThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThoatActionPerformed(evt);
            }
        });

        jButtonClear.setText("Xóa trắng");
        jButtonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClearActionPerformed(evt);
            }
        });

        jButtonXoa.setText("Xóa");
        jButtonXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXoaActionPerformed(evt);
            }
        });

        jButtonSua.setText("Cập nhật");
        jButtonSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSuaActionPerformed(evt);
            }
        });

        jButtonThem.setText("Thêm");
        jButtonThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(213, 213, 213)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 557, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonThem, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(jButtonSua)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonClear)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonThoat))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldLoaiNV, javax.swing.GroupLayout.Alignment.LEADING))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldLoaiNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonThoat)
                    .addComponent(jButtonClear)
                    .addComponent(jButtonXoa)
                    .addComponent(jButtonSua)
                    .addComponent(jButtonThem))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThemActionPerformed
        // TODO add your handling code here:
        if(this.jTextFieldLoaiNV.getText().isEmpty()==true){
            JOptionPane.showMessageDialog(this, "Chưa nhập loại nhân viên");
            return;
        }
        else
        {
            Loainv obj = new Loainv();
            obj.setLoai(this.jTextFieldLoaiNV.getText());
            obj.setGhichu(this.jTextAreaGhiChu.getText());
            
            
            obj.setTinhtrang(1);
            if(LoaiNhanVienDAO.InsertLoainv(obj))
            {
                JOptionPane.showMessageDialog(this, "Thêm thành công!");                                    
                Vector vt = new Vector();            
                vt.add(tbModel.getRowCount()+1);
                vt.add(obj.getLoai());
                vt.add(obj.getGhichu());            
                tbModel.addRow(vt);            
                ls.add(obj);
                this.jTableLoaiNV.setModel(tbModel);
            }
        }         
    }//GEN-LAST:event_jButtonThemActionPerformed

    private void jButtonSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSuaActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            int confirm = JOptionPane.showConfirmDialog(this, "Bạn có muốn cập nhật quy định này không?", "Xác nhận", JOptionPane.YES_NO_OPTION);
            if(confirm == JOptionPane.YES_OPTION){
                int dong = this.jTableLoaiNV.getSelectedRow();            
                Loainv obj = ls.get(dong);
                obj.setLoai(this.jTextFieldLoaiNV.getText());                
                obj.setGhichu(this.jTextAreaGhiChu.getText());                
                obj.setTinhtrang(1);
                if(LoaiNhanVienDAO.UpdateLoainv(obj)){  
                //Date dt = new Date(day.getYear()+1900, day.getMonth()+1, day.getDate());
                    JOptionPane.showMessageDialog(this, "Đã cập nhật");
                    tbModel.setValueAt(obj.getLoai(), dong, 1);                
                    tbModel.setValueAt(obj.getGhichu(), dong, 2);
                }
            }
            else{
                JOptionPane.showMessageDialog(this, "Cập nhật không thành công");
                return;
            }
                
        } catch (Exception ex){
            try {
                throw ex;
            } catch (Exception ex1) {
                Logger.getLogger(JInternalFrameQuyDinh.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }//GEN-LAST:event_jButtonSuaActionPerformed

    private void jButtonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXoaActionPerformed
        // TODO add your handling code here:
         try {
            // TODO add your handling code here:
            int confirm = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa loại nhân viên này không?", "Xác nhận", JOptionPane.YES_NO_OPTION);
            if(confirm == JOptionPane.YES_OPTION){
                int dong = this.jTableLoaiNV.getSelectedRow();            
                Loainv obj = ls.get(dong);
                obj.setLoai(this.jTextFieldLoaiNV.getText());            
                obj.setGhichu(this.jTextAreaGhiChu.getText());            
                obj.setTinhtrang(0);
                if(LoaiNhanVienDAO.DeleteLoainv(obj)){                    
                    tbModel.removeRow(dong);            
                    ls.remove(dong);

                    for(int i = 0;i<this.tbModel.getRowCount();i++)
                    {                
                        this.tbModel.setValueAt(i+1, i, 0);
                    }
                    this.jTableLoaiNV.setModel(tbModel);
                }
            //this.jTableLoaiNV.setModel(this.tbModel);
            }
            else{
                JOptionPane.showMessageDialog(this, "Không xóa được");
                return;
            }
        } catch (Exception ex){
            try {
                throw ex;
            } catch (Exception ex1) {
                Logger.getLogger(JInternalFrameQuyDinh.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }//GEN-LAST:event_jButtonXoaActionPerformed

    private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearActionPerformed
        // TODO add your handling code here:
        this.XoaTrang();
    }//GEN-LAST:event_jButtonClearActionPerformed

    private void jButtonThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonThoatActionPerformed

    private void jTableLoaiNVMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableLoaiNVMouseClicked
        // TODO add your handling code here:
        this.XoaTrang();
        try{                   
            int dong = this.jTableLoaiNV.getSelectedRow();
            if(dong>=0){                            
                String loaiNV = "";                
                String ghiChuQD = "";
                if(this.jTableLoaiNV.getValueAt(dong,1).toString().isEmpty()==false){
                    loaiNV = this.jTableLoaiNV.getValueAt(dong,1).toString();
                    this.jTextFieldLoaiNV.setText(loaiNV);
                }
                
                
                if(this.jTableLoaiNV.getValueAt(dong,2).toString().isEmpty()==false){
                    ghiChuQD = this.jTableLoaiNV.getValueAt(dong,2).toString();                
                    this.jTextAreaGhiChu.setText(ghiChuQD);
                }                                                
            }
        }
        catch (Exception ex){
            //throw ex;
        }
    }//GEN-LAST:event_jTableLoaiNVMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonSua;
    private javax.swing.JButton jButtonThem;
    private javax.swing.JButton jButtonThoat;
    private javax.swing.JButton jButtonXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableLoaiNV;
    private javax.swing.JTextArea jTextAreaGhiChu;
    private javax.swing.JTextField jTextFieldLoaiNV;
    // End of variables declaration//GEN-END:variables
}
